class Solution {
public:
    long long sumDigitDifferences(vector<int>& nums)
    {
        long long ans = 0;
        vector<vector<int>> hash(10, vector<int>(10, 0));
        vector<int> tmp(nums);
        for (auto& a : nums)
        {
            int idx = 0;
            while (a != 0) hash[idx++][a % 10]++, a /= 10;
        }


        for (auto& a : tmp)
        {
            int idx = 0;
            while (a != 0)
            {
                int t = a % 10;
                a /= 10;
                for (int i = 0; i <= 9; i++) if (i != t && hash[idx][i] > 0) ans += hash[idx][i];
                hash[idx++][t]--;
            }
        }
        return ans;

    }
};